#include "notes.h"
#include "ui_notes.h"

notes::notes(QString filename,QHash<QString, NoteT> *hashNote,QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::notes)
{
    ui->setupUi(this);

    this->setWindowTitle("便签");
    this->resize(300,400);
    this->setWindowFlags(Qt::Window | Qt::WindowStaysOnTopHint);

    nt = hashNote;
    title = filename;
    backGroundColor = nt->value(filename).backGroundColor;
    fontColor = nt->value(filename).fontColor;
    this->setcolor();
    ui->textEdit->setText(nt->value(filename).content);
    // 设置上下文菜单策略为自定义
    ui->textEdit->setContextMenuPolicy(Qt::CustomContextMenu);
    // 连接信号和槽
    connect(ui->textEdit, &QTextEdit::customContextMenuRequested, this, &notes::onCustomContextMenu);

    // 计算窗口位置以使其位于屏幕右侧
    int screenWidth = QApplication::primaryScreen()->geometry().width();
    int screeHeight = QApplication::primaryScreen()->geometry().height();
    int windowWidth = this->geometry().width();
    int x = screenWidth - windowWidth;
    int y = screeHeight/2 - 200; // 可以设置窗口在屏幕顶部的位置，或者根据需要调整

    // 设置窗口位置和大小
    this->move(x,y);

}

/**
 * 右键菜单
**/
void notes::onCustomContextMenu(const QPoint &pos){
    // 创建菜单
    QMenu menu(this);
    QAction *changeOpenLink = menu.addAction("打开选中的超链接");
    QAction *menCopy = menu.addAction("复制");
    QAction *menCopyto = menu.addAction("粘贴");
    QAction *menPrint = menu.addAction("打印");
    //分割线
    menu.addAction("--------");
    QAction *changeFontAction = menu.addAction("字体");
    QAction *bold = menu.addAction("加粗");
    QAction *underLine = menu.addAction("下划线");
    QAction *StrikeOut = menu.addAction("删除线");
    QAction *fColor = menu.addAction("颜色");
    QAction *clearFmt = menu.addAction("清除格式");
    QAction *menBackGroundColor = menu.addAction("背景色");
    QAction *menfontColor = menu.addAction("前景色");
    //分割线
    menu.addAction("--------");
    QAction *menfontBlue = menu.addAction("海涛蓝");
    QAction *menfontPurple = menu.addAction("满天星紫");
    QAction *menfontGreen = menu.addAction("荷叶绿");
    QAction *menfontRed = menu.addAction("玉红");

    // 在文本编辑控件上执行菜单
    QAction *selectedAction = menu.exec(ui->textEdit->mapToGlobal(pos));

    // 获取当前选中文本的光标
    QTextCursor cursor = ui->textEdit->textCursor();
    // 创建一个QTextCharFormat对象，用来设置字符格式
    QTextCharFormat fmt;

    // 根据选择的动作执行相应的操作
    if (selectedAction == changeOpenLink){
        QString url = cursor.selectedText().trimmed();
        QDesktopServices::openUrl(url);
    }else if (selectedAction == changeFontAction) {
        // 打开字体对话框来选择新字体
        bool ok;
        QFont font = QFontDialog::getFont(&ok, QFont("国标黑体", 12), this);
        if (ok) {
            fmt.setFont(font);
        }
    } else if (selectedAction == bold) {
        fmt.setFontWeight(QFont::Bold);
    }else if (selectedAction == StrikeOut) {
        fmt.setFontStrikeOut(true);
    }else if (selectedAction == underLine) {
        fmt.setFontUnderline(true);
    }else if (selectedAction == fColor) {
        QColorDialog *colorDialog = new QColorDialog(this);
        colorDialog->setCurrentColor(Qt::red); // 设置初始颜色为红色
        if (colorDialog->exec()) { // 如果用户选择了颜色并点击了“OK”
            QColor c = colorDialog->selectedColor();
            fmt.setForeground(c);
        }
    }else if (selectedAction == clearFmt) {
        //使用 setCharFormat 来设置默认格式
        cursor.setCharFormat(QTextCharFormat());
        return;
    }else if (selectedAction == menCopy) {
        //复制到剪贴板
        QClipboard *clipboard = QApplication::clipboard();
        clipboard->setText(cursor.selectedText());
        return;
    }else if (selectedAction == menCopyto) {
        //复制到TextEdit
        cursor.insertText(QApplication::clipboard()->text());
        return;
    }else if (selectedAction == menPrint) {
        //打印TextEdit的内容
        QPrinter printer(QPrinter::HighResolution);
        ui->textEdit->print(&printer);
        return;
    }else if (selectedAction == menBackGroundColor) {
        // 显示颜色对话框 设置背景色
        QColorDialog dialog;
        backGroundColor = dialog.getColor();
        if(!backGroundColor.isValid())return;
        (*nt)[title].backGroundColor = backGroundColor.name();
        this->setcolor();
        return;
    }else if (selectedAction == menfontColor) {
        //默认前景色
        QColorDialog dialog;
        fontColor = dialog.getColor();
        if(!fontColor.isValid())return;
        (*nt)[title].fontColor = fontColor.name();
        this->setcolor();
        return;
    }else if (selectedAction == menfontBlue) {
        //海涛蓝
        fontColor = "#fff";
        backGroundColor = "#15559a";
        (*nt)[title].fontColor = fontColor.name();
        (*nt)[title].backGroundColor = backGroundColor.name();
        this->setcolor();
        return;
    }else if (selectedAction == menfontPurple) {
        //满天星紫
        fontColor = "#fff";
        backGroundColor = "#2e317c";
        (*nt)[title].fontColor = fontColor.name();
        (*nt)[title].backGroundColor = backGroundColor.name();
        this->setcolor();
        return;
    }else if (selectedAction == menfontGreen) {
        //荷叶绿
        fontColor = "#fff";
        backGroundColor = "#1a6840";
        (*nt)[title].fontColor = fontColor.name();
        (*nt)[title].backGroundColor = backGroundColor.name();
        this->setcolor();
        return;
    }else if (selectedAction == menfontRed) {
        //玉红
        fontColor = "#fff";
        backGroundColor = "#c04851";
        (*nt)[title].fontColor = fontColor.name();
        (*nt)[title].backGroundColor = backGroundColor.name();
        this->setcolor();
        return;
    }
    // 应用新字体到选中文本
    cursor.mergeCharFormat(fmt);

}
notes::~notes()
{
    delete ui;
}

void notes::setcolor(){
    this->setStyleSheet("background-color:"+ this->backGroundColor.name() +
                        "; color: "+ this->fontColor.name() +";");
}
void notes::saveToFile(const QString &fileName, const QString &content) {
    QFile file(fileName);
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        return;
    }

    QTextStream out(&file);
    out << content;
    file.close();
}

void notes::closeEvent(QCloseEvent *event)
{
    // 在这里执行你需要的操作
    QString titleName = ui->textEdit->toPlainText().split("\n").first();
    (*nt)[title].content = ui->textEdit->toHtml();
    if(!titleName.trimmed().isEmpty() && title != titleName){
        (*nt)[title].name = titleName;
        // 获取旧键对应的值
        NoteT value = (*nt).take(title); // take() 方法会移除并返回键对应的值
        // 使用新键插入值
        nt->insert(titleName, value);
        nt->remove(title);
    }
    if(!(*nt)["_nt_"].status.contains("修改文件"+titleName)){
        (*nt)["_nt_"].status += " 修改文件"+titleName;
    }

    emit sigClose();
    event->accept(); // 接受关闭事件，否则窗口不会关闭
}


